package com.zcl.springboot.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 实现一个计时的过滤器
 * Filter是依赖于Servlet容器
 * 通过FilterRegistrationBean 实现过滤器
 * @see  FilterConfig
 */
public class LogCostFilter implements Filter {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogCostFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("进入过滤器中");
       /* HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;
        request1.setCharacterEncoding("utf-8");
        response1.setCharacterEncoding("utf-8");
        System.out.println("request1.getRequestURI()"+request1.getRequestURI());
        if(request1.getRequestURI().indexOf("getUserInfo")>0){
           // response1.sendRedirect("index");
            chain.doFilter(request1,response1);
        }else{
            long start = System.currentTimeMillis();
            chain.doFilter(request1,response1);
            System.out.println("Execute cost="+(System.currentTimeMillis() - start));
        }*/
        long start = System.currentTimeMillis();
        chain.doFilter(request,response);
        System.out.println("Execute cost="+(System.currentTimeMillis() - start));
    }

    @Override
    public void destroy() {

    }
}
